package com.pcbly.web.components.utils.pool;

import java.util.HashMap;
import java.util.Map;

public class InstancePool {

	public static Object get(String className) {
		return _instance._get(className);
	}

	public static void put(String className, Object obj) {
		_instance._put(className, obj);
	}

	private InstancePool() {
		_classPool = new HashMap<String, Object>();
	}

	public InstancePool getInstance() {
		if (_instance == null) {
			synchronized (this) {
				_instance = new InstancePool();
			}
		}
		return _instance;
	}

	private Object _get(String className) {
		className = className.trim();

		Object obj = _classPool.get(className);

		if (obj == null) {
			try {
				obj = Class.forName(className).newInstance();
				_put(className, obj);
			} catch (ClassNotFoundException cnofe) {
				cnofe.printStackTrace();
			} catch (InstantiationException ie) {
				ie.printStackTrace();
			} catch (IllegalAccessException iae) {
				iae.printStackTrace();
			}
		}

		return obj;
	}

	private void _put(String className, Object obj) {
		_classPool.put(className, obj);
	}

	private static InstancePool _instance = null;

	private Map<String, Object> _classPool;

}